package com.woniuxy.carlife.feignservice.impl;

import com.alibaba.fastjson.JSON;
import com.woniuxy.carlife.domain.ServiceInfo;
import com.woniuxy.carlife.dto.ServiceInfoDto;
import com.woniuxy.carlife.dto.ServiceScoreDto;
import com.woniuxy.carlife.feigndto.StoreServiceDto;
import com.woniuxy.carlife.feignservice.StoreFeignService;
import com.woniuxy.carlife.service.ServiceInfoService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author fx
 * @ClassName:StoreFeignServiceImpl
 * @Description: 给门店服务调用返回信息的service实现类
 * @date 2021/4/15 10:41
 **/
@Service
public class StoreFeignServiceImpl implements StoreFeignService {

    @Resource
    private RedisTemplate redisTemplate;

    //查服务详情
    @Resource
    private ServiceInfoService serviceInfoService;

    /**
     * 功能描述〈根据门店id查询计算该门店服务的点赞量、总评论数、根据服务评分计算门店评分〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/15 10:42
     * @throws
     *
     */
    @Override
    public StoreServiceDto findStoreServiceByStoreId(Integer storeId) {
        //先根据门店id查询指定门店下的所有服务
        //先查redis,取出来都是字符串类型，这个key有时效
        List<String> range = redisTemplate.opsForList()
                .range("com.woniuxy:carlife:serviceproject:store:id:" + storeId,
                        0, -1);
        //先声明一个变量设置评论数和平均分等数据
        StoreServiceDto storeDto = new StoreServiceDto();
        //先设置默认值以便后续计算
        storeDto.setStoreId(storeId);
        storeDto.setStoreInfoLike(0);
        storeDto.setStoreInfoScoring(0.0);
        storeDto.setStoreInfoCommentary(0);
        //判断redis整个是否有数据
        if (ObjectUtils.isEmpty(range)){
            //redis没数据就到数据库查
            //调用根据门店id查所有服务的方法
            List<ServiceInfoDto> serviceInfoByStoreId =
                    serviceInfoService.findServiceInfoByStoreId(storeId);
            //循环查出来的数据
            for (ServiceInfoDto s : serviceInfoByStoreId) {
                //先根据服务id从redis中查询点赞数
                String o = (String) redisTemplate.opsForValue()
                        .get("com.woniuxy:carlife:serviceproject:serviceinfo:id:"
                                + s.getServiceInfoId());
                ServiceInfo info;
                if (ObjectUtils.isEmpty(o)){
                    //如果为空可能是已经设置为下架服务，
                    // 但是如果点赞了也要从数据库中取出来统计
                    info =serviceInfoService.getById(s.getServiceInfoId());
                }else{
                    //不为空就转换为对象
                    info = JSON.parseObject(o, ServiceInfo.class);
                }
                //设置点赞数
                storeDto.setStoreInfoLike
                        (storeDto.getStoreInfoLike()+info.getServiceLike());
                //设置评论数根据服务id从reids查
                String s1 = (String) redisTemplate.opsForValue()
                        .get("com.woniuxy:carlife:serviceproject:serviceappraisescore:serviceid:"
                                + s.getServiceInfoId());
                //判断是否为空
                if (ObjectUtils.isEmpty(s1)){
                    //没有数据就设置为初始值，说明这个服务还没有被评论过
                    //评论数不变，不设置
                    //评分设置为默认5分
                    storeDto.setStoreInfoScoring(storeDto.getStoreInfoScoring()+5);
                    //没被评论过也就不需要存到redis了
                }else{
                    //有数据说明这个服务被评论过，要拿到评论数和平均分
                    //字符串转对象
                    ServiceScoreDto scoreDto = JSON.parseObject(s1, ServiceScoreDto.class);
                    //设置评论数，从reids查到的当前服务的评论总数加上之前其他服务的评论总数
                    storeDto.setStoreInfoCommentary(storeDto.getStoreInfoCommentary()
                            +scoreDto.getCommentsNum());
                    //设置评分
                    storeDto.setStoreInfoScoring(storeDto.getStoreInfoScoring()+scoreDto.getServiceScoreSum());
                }
                //把这个服务id存到redis
                redisTemplate.opsForList()
                        .leftPush("com.woniuxy:carlife:serviceproject:store:id:" + storeId,
                                "serviceinfo:"+s.getServiceInfoId());
            }
        }else{
            //有数据就循环
            for (String s : range) {
                //判断每一个值都是有效的
                if (s!=null){
                    System.out.println(s+"==================================");
                    //转换成Integer类型
                    int i = Integer.parseInt(s.substring(s.lastIndexOf(":")+1));
                    //从redis中根据服务id取服务评论的统计
                    //设置评论数根据服务id从reids查
                    String s1 = (String) redisTemplate.opsForValue()
                            .get("com.woniuxy:carlife:serviceproject:serviceappraisescore:serviceid:"
                                    + i);
                    //转换成对象
                    ServiceScoreDto scoreDto = JSON.parseObject
                            (s1, ServiceScoreDto.class);
                    //设置评论数，从reids查到的当前服务的评论总数加上之前其他服务的评论总数
                    storeDto.setStoreInfoCommentary(storeDto.getStoreInfoCommentary()
                            +scoreDto.getCommentsNum());
                    //设置评分
                    storeDto.setStoreInfoScoring
                            (storeDto.getStoreInfoScoring()+scoreDto.getServiceScoreSum());
                    //获取点赞量
                    String o = (String) redisTemplate.opsForValue()
                            .get("com.woniuxy:carlife:serviceproject:serviceinfo:id:"
                                    + i);
                    ServiceInfo info = JSON.parseObject(o, ServiceInfo.class);
                    storeDto.setStoreInfoLike(info.getServiceLike());
                }
            }
        }
        if (storeDto.getStoreInfoScoring()!=0&&storeDto.getStoreInfoCommentary()!=0){
            System.out.println(storeDto.getStoreInfoScoring());
            System.out.println(storeDto.getStoreInfoCommentary());
            //计算评论平均分
            storeDto.setStoreInfoScoring(storeDto.getStoreInfoScoring()/
                    storeDto.getStoreInfoCommentary());
        }
        //设置过期时间，来个1分钟
        redisTemplate.expire("com.woniuxy:carlife:serviceproject:store:id:" + storeId,
                90, TimeUnit.SECONDS);
        return storeDto;
    }
}
